function al(){}
al.url = null;					//请求的URL
al.value = 'CD';				//键值对时 KEY
al.label = 'NM';				//键值对时 VALUE
al.async = true;				//是否异步请求
al.data = null;					//请求参数
al.user = null;					//用户名
al.password = null;				//密码
al.success = function(o){};		//请求成功时的回调函数 与callback相同
al.callback = function(o){};	//请求成功时的回调函数 与success相同
al.fail = function(data,msg){	//请求失败的回调函数
	if(art){
		art.dialog({content:msg,time:2});
	}
}
al.type = 'POST';				//请求方法类型
al.form = null;					//提交的form
al.xhr = null;					//xhr
al.dataType = 'json';			//返回的数据类型
al.json = null;					//返回的数据

var _lock = null;
al.init=function(config){
	if(config['success']){al.success = config['success'];}
	if(config['callback']){al.callback = config['callback'];}
	if(config['fail']){al.fail = config['fail'];}
	if(typeof config['async'] == "undefined") {al.async = config['async'];}
	if(config['type']) {al.method = config['type'];}
	if(config['url']) {al.url = config['url'];}
	if(config['data']) {al.data = config['data'];}
	if(config['dataType']) {al.dataType = config['dataType'];}
}
al.submit = function(frm,config){
	if(!frm){return;}
	al.init(config);
	al.lock();
	$('#'+frm).ajaxSubmit({
		type:'post',
		dataType: 'json',
		success:function(json){
			var result = json['result'];
			var message = json['message'];
			var data;
			var type = json['type'];
			al.json = json;
			al._ajax_success();
		},
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
	   		al._ajax_error(XMLHttpRequest, textStatus, errorThrown);
	   }
	});
	
};
al.ajax = function(config){
	al.init(config);
	$.ajax({
	   type: al.type,
	   url: al.url,
	   data: al.data,
	   dataType: al.dataType,
	   success: function(json){
	   		al.json = json;
			al._ajax_success();
	   },
	   error:function(XMLHttpRequest, textStatus, errorThrown) {
	   		al._ajax_error(XMLHttpRequest, textStatus, errorThrown);
	   }
	});
};

/**
*锁屏 依赖与artdialog
*/
al.lock = function(time){
	if(_lock){
		_lock.titme = time;
	}else{
		_lock = art.dialog({
			show:false,
			lock:true,
			time:time
		});
	}
}
/**
*解锁 依赖与artdialog
*/
al.unlock = function(time){
	if(_lock && _lock.close){
		_lock.close();
	}
	_lock = null;
}
al._ajax_success = function (){
	al.unlock();
	var result = al.json['result'];
	var message = al.json['message'];
	var data;
	var type = al.json['type'];
	
	//解析数据
	if(type=='string' || type=='number'){
		data = al.json['data'];
	}else if(type=='map'){
		data = al.json['data'];
	}else if(type == 'list'){
		var tmp = al.json['data'];
		if(tmp.length == 1){
			data = tmp[0];
			data[0] = tmp[0];
			data.length = 1;
		}else{
			data = tmp;
		}
	}
	//附加操作方法
	if(data){
		data.get=function(idx,key){
			if(!key){
				key = idx;
				idx = 0;
			}
			if(data.length && idx<data.length){
				return data[idx][key];
			}else{
				return null;
			}
		};
	}
	if(result){
		//函数回调
		var success = al.success;
		if(success){
			success(data,message);
		}
	}else{
		var fail = al.fail;
		if(fail){
			fail(data,message);
		}
		if(al.json['url']){
			window.document.location.target = '_top';
			window.document.location.href = json['url'];
		}
		
	}
	
	var callback = al.callback;
	if(callback){
		callback(result,data,message);
	}
};
al._ajax_error = function(XMLHttpRequest, textStatus, errorThrown){
	//al.lock();
	if(art){
		art.dialog({content:XMLHttpRequest.responseText});
	}else{
		alert("状态:"+textStatus+"\n消息:"+XMLHttpRequest.responseText);
	}
};

al.tips = function(content, time){
	 return artDialog({
	        id: 'Tips',
	        title: false,
	        cancel: false,
	        fixed: true
	    })
	    .content('<div style="padding: 0 1em;">' + content + '</div>')
	    .time(time || 1);
};
	
al.trim = function(src){
	if(src){
		return src.replace(/(^\s*)|(\s*$)/g, ""); 
	}else{
		return "";
	}
}; 


/**
*金额转大写
*/
function cfMoneyFormat(n) {
	if(n*1 == 0){
		return "零";
	}
	if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
		return "数据非法";
	var unit = "千百拾亿千百拾万千百拾元角分", str = "";
		n += "00";
	var p = n.indexOf('.');
	if (p >= 0)
		n = n.substring(0, p) + n.substr(p+1, 2);
		unit = unit.substr(unit.length - n.length);
	for (var i=0; i < n.length; i++)
		str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
	return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
}

function cfNumberFormat (number, decimals, dec_point, thousands_sep) {
	    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
	    var n = !isFinite(+number) ? 0 : +number,
	        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	        s = '',
	        toFixedFix = function (n, prec) {
	            var k = Math.pow(10, prec);
	            return '' + Math.round(n * k) / k;
	        };
	    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
	    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	    if (s[0].length > 3) {
	        s[0] = s[0].replace(/B(?=(?:d{3})+(?!d))/g, sep);
	    }
	    if ((s[1] || '').length < prec) {
	        s[1] = s[1] || '';
	        s[1] += new Array(prec - s[1].length + 1).join('0');
	    }
	    return s.join(dec);
	}
	
function cfCheckInt(val){
	return /^[\d]+$/.test(val);
}
//判断参数是否为数据类型
function cfCheckNumber(num){
	var exp = /^\d{1,}(.{0,1}\d{1,}||\d{0,})$/;
	return exp.test(num);
}
//空格处理
function cfTrim(str){
	var strValue;
	strValue = "" + str;
	return strValue.replace(/(^\s*)|(\s*$)/g, '');
}
function cfDel(url,cd){
	art.dialog({
	content: '确实在删除当前数据?',
	ok: function(){
		al.ajax({
			url:url,
			data:{cd:cd},
			callback:function(result,data,msg){
				if(!result){
					art.dialog({title:'删除失败',content:'删除失败:'+msg,time:3});
				}else{
					$('#tr'+cd).remove();
					art.dialog({title:'删除成功',content:'删除成功',time:1});
				}
			}
		});
	}
    });
}